package com.bsb.acc.controller;

import com.bsb.acc.domain.AccShortcutFieldConfig;
import com.bsb.acc.dto.AccShortcutFieldConfigAllDto;
import com.bsb.acc.dto.AccShortcutFieldConfigDto;
import com.bsb.acc.oms.web.domain.ResponseWrapper;
import com.bsb.acc.service.AccShortcutFieldConfigService;
import com.bsb.acc.utils.FileUploadUtils;
import java.io.IOException;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

/**
 * 快捷栏位配置控制层
 * @Author: ligc
 * on 2019/2/14
 * @Description
 */
@Controller
@RequestMapping(value = "accShortcutFieldConfig")
public class AccShortcutFieldConfigController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private AccShortcutFieldConfigService accShortcutFieldConfigService;

    @RequestMapping(value = "publishAccShortcutFieldConfig", method = RequestMethod.POST, produces = {
            MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity<ResponseWrapper<String>> publishAccShortcutFieldConfig(@RequestBody AccShortcutFieldConfigAllDto accShortcutFieldConfigAllDto)
            throws IOException {
        logger.info("发布快捷栏位");
        try {
           accShortcutFieldConfigService.publishAccShortcutFieldConfig(accShortcutFieldConfigAllDto);
        } catch (Exception e) {
            throw e;
        }
        return new ResponseEntity<ResponseWrapper<String>>(
                ResponseWrapper.ok("success").build(), HttpStatus.OK);
    }

    @RequestMapping(value = "uploadAccShortcutFieldConfig", method = RequestMethod.POST, produces = {
            MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity<ResponseWrapper<AccShortcutFieldConfig>> uploadAccShortcutFieldConfig(
            @RequestParam MultipartFile file, HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        logger.info("[uploadImageUrl]上传图片：file={}", file);
        AccShortcutFieldConfig accShortcutFieldConfig = null;
        String id = request.getParameter("id");
        try {
            String returnDir = FileUploadUtils.uploadImage(file.getBytes());
            accShortcutFieldConfig = accShortcutFieldConfigService.save(returnDir, "user", id);
        } catch (Exception e) {
            throw e;
        }
        return new ResponseEntity<ResponseWrapper<AccShortcutFieldConfig>>(
                ResponseWrapper.ok(accShortcutFieldConfig).build(), HttpStatus.OK);
    }

    /**
     * 根据类状态展示快捷栏，1：已发布的  2.停用的
     */
    @RequestMapping(value = "getAccShortcutFieldConfigByStatus", method = RequestMethod.POST, produces = {
            MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity<ResponseWrapper<List<AccShortcutFieldConfig>>> ugetAccShortcutFieldConfigByType(
            Integer status)
            throws IOException {
        List<AccShortcutFieldConfig> list = null;
        try {
            list = accShortcutFieldConfigService.getAccShortcutFieldConfigByType(status);
        } catch (Exception e) {
            throw e;
        }
        return new ResponseEntity<ResponseWrapper<List<AccShortcutFieldConfig>>>(
                ResponseWrapper.ok(list).build(), HttpStatus.OK);
    }

    /**
     * 更新url 备注等
     * @param accShortcutFieldConfigDto
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "updateByFiled", method = RequestMethod.POST, produces = {
            MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity<ResponseWrapper<AccShortcutFieldConfig>> updateByFileds(
            AccShortcutFieldConfigDto accShortcutFieldConfigDto)
            throws IOException {
        AccShortcutFieldConfig entity = null;
        try {
            entity = accShortcutFieldConfigService.updateByFileds(accShortcutFieldConfigDto);
        } catch (Exception e) {
            throw e;
        }
        return new ResponseEntity<ResponseWrapper<AccShortcutFieldConfig>>(
                ResponseWrapper.ok(entity).build(), HttpStatus.OK);
    }

}
